package net.wanglu.www.zzz.utils;

import java.io.InputStream;

import opennlp.tools.chunker.ChunkerME;
import opennlp.tools.chunker.ChunkerModel;
import opennlp.tools.coref.mention.JWNLDictionary;
import opennlp.tools.namefind.NameFinderME;
import opennlp.tools.namefind.TokenNameFinderModel;
import opennlp.tools.postag.POSModel;
import opennlp.tools.postag.POSTaggerME;
import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.tools.sentdetect.SentenceModel;
import opennlp.tools.tokenize.Tokenizer;
import opennlp.tools.tokenize.TokenizerME;
import opennlp.tools.tokenize.TokenizerModel;

public class OpenNLPUtils {

	private static ChunkerME chunker = null;
	private static POSTaggerME tagger = null;
	private static Tokenizer tokenizer = null;
	private static SentenceDetectorME sentenceDetectorME = null;
	private static NameFinderME personFinderME = null;
	private static NameFinderME locationFinderME = null;
	private static NameFinderME organizationFinderME = null;
	
	public static ChunkerME getChunkerME() throws Exception {
		if ( chunker == null ) {
			InputStream modelIn = OpenNLPUtils.class.getResourceAsStream("en-chunker.bin");
			ChunkerModel model = new ChunkerModel(modelIn);
			chunker = new ChunkerME(model);
		}
		
		return chunker;
	}
	
	public static POSTaggerME getPOSTaggerME() throws Exception {
		if ( tagger == null) {
			InputStream modelIn = OpenNLPUtils.class.getResourceAsStream("en-pos-maxent.bin");
			POSModel model = new POSModel(modelIn);
			tagger = new POSTaggerME(model);
		}
		return tagger;
	}
	
	public static Tokenizer getTokenizer() throws Exception {
		if ( tokenizer == null ) {
			InputStream in = OpenNLPUtils.class.getResourceAsStream("en-token.bin");
			TokenizerModel model1 = new TokenizerModel(in);
			tokenizer = new TokenizerME(model1);			
		}
		return tokenizer;
	}
	
	public static SentenceDetectorME getSentenceDetectorME() throws Exception {
		if ( sentenceDetectorME == null) {
			InputStream in = OpenNLPUtils.class.getResourceAsStream("en-sent.bin");
			SentenceModel model = new SentenceModel(in);
			sentenceDetectorME = new SentenceDetectorME(model);
		}
		return sentenceDetectorME;
	}
	
	public static NameFinderME getPersonFinderME() throws Exception {
		if ( personFinderME == null) {
			InputStream in = OpenNLPUtils.class.getResourceAsStream("en-ner-person.bin");
			TokenNameFinderModel model = new TokenNameFinderModel(in);
			personFinderME = new NameFinderME(model);
		}
		return personFinderME;
	}
	
	public static NameFinderME getLocationFinderME() throws Exception {
		if ( locationFinderME == null) {
			InputStream in = OpenNLPUtils.class.getResourceAsStream("en-ner-location.bin");
			TokenNameFinderModel model = new TokenNameFinderModel(in);
			locationFinderME = new NameFinderME(model);
		}
		return locationFinderME;
	}
	
	public static NameFinderME getOrganizationFinderME() throws Exception {
		if ( organizationFinderME == null) {
			InputStream in = OpenNLPUtils.class.getResourceAsStream("en-ner-organization.bin");
			TokenNameFinderModel model = new TokenNameFinderModel(in);
			organizationFinderME = new NameFinderME(model);
		}
		return organizationFinderME;
	}
	
	private static JWNLDictionary jd = null;
	public static JWNLDictionary getJWNLDictionary() throws Exception {
		if ( jd == null) {
			JwnlUtils.initJWNL();
			jd = new JWNLDictionary("C:\\Program Files\\WordNet\\2.1\\dict");
		}
		return jd;
	}
}
